package personal.wang.juc.juc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @className: PrintRotation1
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/3/21 17:38
 */
public class PrintRotation1 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(11);
        queue.put(1);
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        executorService.execute(()->{
            while(true){
                try {
                    Integer take = queue.take();
                    if(take%2 == 0){
                        System.out.println(take);
                        queue.put(take+1);
                    }else{
                        queue.put(take);
                    }
                    if(take >=10){
                        break;
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        executorService.execute(()->{
            while(true){
                try {
                    Integer take = queue.take();
                    if(take%2 != 0){
                        System.out.println(take);
                        queue.put(take+1);
                    }else{
                        queue.put(take);
                    }
                    if(take >=9){
                       break;
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        executorService.shutdown();
    }
}
